package com.lbs.jiepiaisi.biz;

import android.content.Intent;
import android.util.Log;

import com.lbs.jiepiaisi.bean.EquipmentEntity;
import com.lbs.jiepiaisi.bean.EquipmentEqNoEntity;
import com.lbs.jiepiaisi.bean.TrackEntity;
import com.lbs.jiepiaisi.bean.VinList;
import com.lbs.jiepiaisi.manager.App;
import com.lbs.jiepiaisi.parser.trackAccountParser;
import com.lbs.jiepiaisi.utils.Constant;
import com.lbs.jiepiaisi.utils.HttpUtil;
import com.lbs.jiepiaisi.utils.UrlUtil;

import okhttp3.FormBody;
import okhttp3.Response;

/**
 *
 * 时间：2017/9/14 16:26
 * 作用：实时跟踪
 */

public class TrackBiz {
    //查询实时跟踪
    public void trackBiz(String eqNo, final EquipmentEntity.Result.Item item, final EquipmentEqNoEntity.Result noItem, final String flag, final boolean isOne){
        FormBody formBody = new FormBody
                .Builder()
                .add("eqNo",eqNo)
                .build();
        HttpUtil.getHttpUtil().post(UrlUtil.ACCOUNT_TRACK, formBody, new HttpUtil.Listener() {
            @Override
            public void success(Response response) {
                TrackEntity trackEntity=new TrackEntity();
                try{
                    //返回结果
                    String jsonString = response.body().string();
                    Log.e("track",jsonString);
                    if(isOne){
                        trackEntity= trackAccountParser.parser(jsonString);
                    }else {
                        trackEntity= trackAccountParser.parser2(jsonString);
                    }

                }catch (Exception e){
                    trackEntity.setStatus(Constant.STATUS_NETWORK_ERROR);
                    e.printStackTrace();
                }finally {
                    Intent intent = new Intent(flag);
                    intent.putExtra(Constant.KEY_DATA, trackEntity);
                    if(item != null){
                        intent.putExtra(Constant.KEY_DATA2, item);
                    }
                    if(noItem != null){
                        intent.putExtra(Constant.KEY_DATA2, noItem);
                    }
                    App.instance.sendBroadcast(intent);
                }
            }

            @Override
            public void failed() {
                TrackEntity trackEntity=new TrackEntity();
                trackEntity.setStatus(Constant.STATUS_NO_NET);
                Intent intent = new Intent(flag);
                intent.putExtra(Constant.KEY_DATA, trackEntity);
                App.instance.sendBroadcast(intent);
            }

            @Override
            public void noNetwork() {
                TrackEntity trackEntity=new TrackEntity();
                trackEntity.setStatus(Constant.STATUS_NETWORK_ERROR);
                Intent intent = new Intent(flag);
                intent.putExtra(Constant.KEY_DATA, trackEntity);
                App.instance.sendBroadcast(intent);
            }
        });
    }

    //查询实时跟踪
    public void trackDeviceBiz(String eqNo, final EquipmentEntity.Result.Item item, final EquipmentEqNoEntity.Result noItem,final String flag){
        FormBody formBody = new FormBody
                .Builder()
                .add("page","1")
                .add("size","1")
                .add("status","")
                .add("str",eqNo)
                .add("deptId","")
                .build();
        HttpUtil.getHttpUtil().post(UrlUtil.ACCOUNT_TRACK_PHOTO, formBody, new HttpUtil.Listener() {
            @Override
            public void success(Response response) {
                TrackEntity trackEntity=new TrackEntity();
                try{
                    //返回结果
                    String jsonString = response.body().string();
                    Log.e("trackDeviceBiz",jsonString);
                    trackEntity= trackAccountParser.parser2(jsonString);
                    Log.e("trackDeviceBiz",trackEntity.getResults().get(0).getStatus());
                }catch (Exception e){
                    trackEntity.setStatus(Constant.STATUS_NETWORK_ERROR);
                    e.printStackTrace();
                }finally {
                    Intent intent = new Intent(flag);
                    intent.putExtra(Constant.KEY_DATA, trackEntity);
                    if(item != null){
                        intent.putExtra(Constant.KEY_DATA2, item);
                    }
                    if(noItem != null){
                        intent.putExtra(Constant.KEY_DATA2, noItem);
                    }
                    App.instance.sendBroadcast(intent);
                }
            }

            @Override
            public void failed() {
                TrackEntity trackEntity=new TrackEntity();
                trackEntity.setStatus(Constant.STATUS_NO_NET);
                Intent intent = new Intent(flag);
                intent.putExtra(Constant.KEY_DATA, trackEntity);
                App.instance.sendBroadcast(intent);
            }

            @Override
            public void noNetwork() {
                TrackEntity trackEntity=new TrackEntity();
                trackEntity.setStatus(Constant.STATUS_NETWORK_ERROR);
                Intent intent = new Intent(flag);
                intent.putExtra(Constant.KEY_DATA, trackEntity);
                App.instance.sendBroadcast(intent);
            }
        });
    }

    //查询实时跟踪
    public void trackBizNoCar(String eqNo, final EquipmentEntity.Result.Item item, final VinList.Vin noItem, final String flag, final boolean isOne){
        FormBody formBody = new FormBody
                .Builder()
                .add("eqNo",eqNo)
                .build();
        HttpUtil.getHttpUtil().post(UrlUtil.ACCOUNT_TRACK, formBody, new HttpUtil.Listener() {
            @Override
            public void success(Response response) {
                TrackEntity trackEntity=new TrackEntity();
                try{
                    //返回结果
                    String jsonString = response.body().string();
                    Log.e("track",jsonString);
                    if(isOne){
                        trackEntity= trackAccountParser.parser(jsonString);
                    }else {
                        trackEntity= trackAccountParser.parser2(jsonString);
                    }

                }catch (Exception e){
                    trackEntity.setStatus(Constant.STATUS_NETWORK_ERROR);
                    e.printStackTrace();
                }finally {
                    Intent intent = new Intent(flag);
                    intent.putExtra(Constant.KEY_DATA, trackEntity);
                    if(item != null){
                        intent.putExtra(Constant.KEY_DATA2, item);
                    }
                    if(noItem != null){
                        intent.putExtra(Constant.KEY_DATA2, noItem);
                    }
                    App.instance.sendBroadcast(intent);
                }
            }

            @Override
            public void failed() {
                TrackEntity trackEntity=new TrackEntity();
                trackEntity.setStatus(Constant.STATUS_NO_NET);
                Intent intent = new Intent(flag);
                intent.putExtra(Constant.KEY_DATA, trackEntity);
                App.instance.sendBroadcast(intent);
            }

            @Override
            public void noNetwork() {
                TrackEntity trackEntity=new TrackEntity();
                trackEntity.setStatus(Constant.STATUS_NETWORK_ERROR);
                Intent intent = new Intent(flag);
                intent.putExtra(Constant.KEY_DATA, trackEntity);
                App.instance.sendBroadcast(intent);
            }
        });
    }
}
